__author__ = 'wangjc'

import platform
import paramiko

class BaseUtil:

    def getPlatform(self):
        if platform.system().lower() == 'windows':
            return 'windows'
        elif platform.system().lower() == 'linux':
            return 'linux'

    # 创建ssh连接到指定服务器
    def excuteSsh(self, envInfo):
        # 建立连接
        trans = paramiko.Transport((envInfo[0][0], 22))
        trans.connect(username=envInfo[0][2], password=envInfo[0][3])

        return trans

    # 创建ssh执行命令
    def excuteCmd(self, trans,cmd):
        # 将sshclient的对象的transport指定为以上的trans
        ssh = paramiko.SSHClient()
        ssh._transport = trans
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command(cmd)
        # msg = False
        # for r in ssh_stdout.readlines():
        #     if 'Welcome' in r:
        #         msg = True
        #         break
        # result = [r.strip() for r in ssh_stdout.readlines()]  # 执行结果
        # 关闭连接
        # trans.close()
        return ssh_stdout.readlines()